import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'

export default defineConfig({
  plugins: [
    vue(),

    // ✅ 插件中实现根路径跳转
    {
      name: 'redirect-root-to-login',
      configureServer(server) {
        server.middlewares.use((req, res, next) => {
          if (req.url === '/') {
            res.statusCode = 302
            res.setHeader('Location', '/login.html')
            res.end()
          } else {
            next()
          }
        })
      }
    }
  ],

  build: {
    rollupOptions: {
      input: {
        login: path.resolve(__dirname, 'public/login.html'),
        index: path.resolve(__dirname, 'public/index.html'),
        gate: path.resolve(__dirname, 'public/gate.html')
      }
    }
  },

  resolve: {
    alias: {
      '@': path.resolve(__dirname, 'src')
    }
  },

  server: {
    proxy: {
      '/api': {
        target: 'http://localhost:8090',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, ''),
      }
    }
  }
})
